#!/bin/bash

# 创建目录结构
mkdir -p /home/project/{src,logs,reports}

# 创建日志文件并写入内容
if [ ! -f /home/project/logs/access.log ]; then
  cat <<EOF > /home/project/logs/access.log
INFO: User admin logged in at 2025-06-15
ERROR: Disk full on device /dev/sda1
INFO: Backup completed successfully
ERROR: Connection timeout from IP 192.168.1.5
EOF
fi

# 提取包含 "ERROR" 的日志并保存到 reports/error_report 文件中
grep "ERROR" /home/project/logs/access.log > /home/project/reports/error_report

# 统计 ERROR 出现的次数并追加到报告末尾
echo "Total ERROR count: $(grep -c 'ERROR' /home/project/logs/access.log)" >> /home/project/reports/error_report

# 设置 reports 目录权限为仅拥有者可读写执行（700）
chmod 700 /home/project/reports

# 将 logs 目录打包为 logs.tar.gz（含压缩）
tar -czf /home/project/logs.tar.gz -C /home/project logs
